﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using CFrame.Manager;
using System.Reflection;
using CFrame.Tools;

/*
 * 脚本功能：游戏调度中心
 * 编写日期：2018.12.12
 * 编写人：HYZ
 */
namespace CFrame.Common
{
    public class GameMiddle : MonoBehaviour
    {
        private static GameMiddle instance;
        public static GameMiddle Instance
        {
            get
            {
                if(instance == null)
                {
                    instance = GameObject.FindObjectOfType<GameMiddle>();
                }
                if(instance == null)
                {
                    GameObject go = new GameObject("GameMiddle");
                    instance = go.AddComponent<GameMiddle>();
                }
                return instance;
            }
        }

        /// <summary>
        /// 存储所有的Manager
        /// </summary>
        private Dictionary<string, BaseManager> managerDict;
        

        void Awake()
        {
            if (instance == null)
                instance = this;

        }

        void Start()
        {
            foreach (var manager in managerDict.Values)
            {
                manager.OnInit();
            }
        }

        void Update()
        {
            foreach (var manager in managerDict.Values)
            {
                manager.OnUpdate();
            }
        }

        void OnDestroy()
        {
            foreach (var manager in managerDict.Values)
            {
                manager.OnDestroy();
            }
        }

        /// <summary>
        /// 获取管理器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetManager<T>() where T:BaseManager
        {
            BaseManager manager;
            string key = typeof(T).ToString();
            try
            {
                if (managerDict.TryGetValue(key, out manager) == false)
                {
                    Debuger.LogError("没有注册对象:" + key);
                    return null;
                }
                return manager as T;
            }
            catch(Exception e)
            {
                Debuger.LogWarning("GetManager Error:【" + key + "】 --> " + e);
            }
            return null;
        }

        /// <summary>
        /// 注册管理器，用到了哪些管理器在初始化之前要先把注册进来
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void AddManager<T>() where T : BaseManager
        {
            string key = typeof(T).ToString();
            try
            {
                if (!managerDict.ContainsKey(key))
                {
                    Type type = Type.GetType(key);
                    object t = type.Assembly.CreateInstance(key);
                    ((T)t).Game = this;
                    managerDict.Add(key, (T)t);
                }
            }catch(Exception e)
            {
                Debuger.LogWarning("AddManager Error:【" + key + "】 --> " + e);
            }
        }
    }
}
